<template>
  <div class="container">
    <el-card class="box-card">
      <span class="pink">说明：目前支持学科和关键字条件筛选</span>
      <el-button type="success"
                 icon="el-icon-edit"
                 style="float: right"
                 @click="addQuestion">新增试题</el-button>
      <!-- 表单 -->
      <el-form v-model="form"
               :model="form"
               ref="form"
               :inline="true"
               class="demo-form-inline">
        <el-form-item label="学科"
                      prop="subjectID">
          <el-select v-model="form.subjectID"
                     placeholder="请选择"
                     @change="changeBranch">
            <el-option :label="item.label"
                       :value="item.value"
                       v-for="item in subjectIDs"
                       :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级目录"
                      prop="catalogID">
          <el-select v-model="form.catalogID"
                     placeholder="请选择">
            <el-option :label="item.directoryName"
                       :value="item.id"
                       v-for="item in catalogIDs"
                       :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签"
                      prop="tag">
          <el-select v-model="form.tag"
                     placeholder="请选择">
            <el-option :label="item.tagName"
                       :value="item.id"
                       v-for="item in tags"
                       :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键字"
                      prop="keyword">
          <el-input v-model="form.keyword"></el-input>
        </el-form-item>
        <el-form-item label="试题类型"
                      prop="questionType">
          <el-select v-model="form.questionType"
                     placeholder="请选择">
            <el-option label="单选"
                       value="1"></el-option>
            <el-option label="多选"
                       value="2"></el-option>
            <el-option label="简答"
                       value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="难度"
                      prop="difficulty">
          <el-select v-model="form.difficulty"
                     placeholder="请选择">
            <el-option label="简单"
                       value="1"></el-option>
            <el-option label="一般"
                       value="2"></el-option>
            <el-option label="困难"
                       value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="方向"
                      prop="direction">
          <el-select v-model="form.direction"
                     placeholder="请选择">
            <el-option :label="item"
                       :value="item"
                       v-for="item in directions"
                       :key="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="录入人"
                      prop="creatorID">
          <el-select v-model="form.creatorID"
                     placeholder="请选择">
            <el-option :label="item.username"
                       :value="item.id"
                       v-for="item in creatorIDs"
                       :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题目备注"
                      prop="remarks">
          <el-input v-model="form.remarks"></el-input>
        </el-form-item>
        <el-form-item label="企业简称"
                      prop="shortName">
          <el-input v-model="form.shortName"></el-input>
        </el-form-item>
        <el-form-item label="城市"
                      class="city"
                      prop="city">
          <el-select style="margin-right: 10px"
                     v-model="form.city"
                     placeholder="请选择"
                     @change="changeCity">
            <el-option :label="item.city"
                       :value="item.city"
                       v-for="item in citys"
                       :key="item.city"></el-option>
          </el-select>
          <el-select v-model="form.province"
                     placeholder="请选择">
            <el-option :label="item"
                       :value="item"
                       v-for="item in provinces"
                       :key="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button @click="resetForm('form')">清除</el-button>
          <el-button type="primary"
                     @click="serQuestion">搜索</el-button>
        </el-form-item>
      </el-form>

      <!-- 选项卡 -->
      <el-tabs v-model="activeName"
               type="card"
               @tab-click="switchTad">
        <el-tab-pane label="全部"
                     name="first"></el-tab-pane>
        <el-tab-pane label="待审核"
                     name="0"> </el-tab-pane>
        <el-tab-pane label="已审核"
                     name="1"> </el-tab-pane>
        <el-tab-pane label="已拒绝"
                     name="2"> </el-tab-pane>
      </el-tabs>

      <!-- 表格 -->
      <el-alert type="info" :title="`数据一共${total}条`" :closable="false" show-icon></el-alert>
      <el-table :data="questions" style="width: 100%" class="formtable">
        <el-table-column prop="number" label="试题编号" width="150">
        </el-table-column>
        <el-table-column prop="subject"
                         label="学科"
                         width="120">
        </el-table-column>
        <el-table-column prop="catalog"
                         label="目录"
                         width="120">
        </el-table-column>
        <el-table-column label="题型"
                         width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.questionType === '3'">{{ "简答" }}</span>
            <span v-if="scope.row.questionType === '2'">{{ "多选" }}</span>
            <span v-if="scope.row.questionType === '1'">{{ "单选" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="题干"
                         width="250">
          <template slot-scope="scope">
            <span v-html="scope.row.question"></span>
          </template>
        </el-table-column>
        <el-table-column prop=""
                         label="录入时间"
                         width="220">
          <template slot-scope="scope">
            {{
              scope.row.addDate | parseTimeByString("{y}-{m}-{d} {h}:{i}:{s}")
            }}
          </template>
        </el-table-column>
        <el-table-column label="难度"
                         width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.difficulty === '3'">{{ "困难" }}</span>
            <span v-if="scope.row.difficulty === '2'">{{ "一般" }}</span>
            <span v-if="scope.row.difficulty === '1'">{{ "简单" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="creator"
                         label="录入人"
                         width="150">
        </el-table-column>
        <el-table-column label="审核状态"
                         width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.chkState === 0">{{ "待审核" }}</span>
            <span v-if="scope.row.chkState === 1">{{ "已审核" }}</span>
            <span v-if="scope.row.chkState === 2">{{ "已拒绝" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="chkRemarks"
                         label="审核意见"
                         width="150">
        </el-table-column>
        <el-table-column prop="chkUser"
                         label="审核人"
                         width="100">
        </el-table-column>
        <el-table-column label="发布状态"
                         width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.publishState === 0 && scope.row.chkState === 1">{{ "已下架" }}</span>
            <span v-if="scope.row.publishState === 1 && scope.row.chkState === 1">{{ "已发布" }}</span>
            <span v-if="scope.row.chkState === 0 || scope.row.chkState === 2">{{ "待发布" }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right"
                         label="操作"
                         width="200">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)"
                       type="text"
                       size="small">预览</el-button>
            <el-button type="text"
                       size="small"
                       :disabled="scope.row.chkState === 1 || scope.row.chkState === 2"
                       @click="audit(scope.row.id)">审核</el-button>
            <el-button type="text"
                       size="small"
                       :disabled="scope.row.publishState === 1"
                       @click="modify(scope.row.id)">修改</el-button>
            <el-button type="text"
                       size="small"
                       @click="shelves(scope.row)">{{
              scope.row.publishState === 0 ? "上架" : "下架"
            }}</el-button>
            <el-button type="text"
                       size="small"
                       @click="del(scope.row)"
                       :disabled="scope.row.publishState === 1">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="block">
        <el-pagination background
                       layout="total, prev, pager, next, jumper"
                       :total="total"
                       current-change="changepage"
                       :page="form.page"
                       :page-size="form.pagesize">
        </el-pagination>
      </div>
    </el-card>

    <!-- 审核弹出层 -->
    <el-dialog title="添加素材"
               :visible.sync="dialogVisible"
               width="450px"
               height="500px">
      <el-form>
        <el-form-item>
          <el-radio-group v-model="audits.chkState">
            <el-radio label="1">同意</el-radio>
            <el-radio label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-input type="textarea"
                    placeholder="请输入审核意见"
                    v-model.trim="audits.chkRemarks"></el-input>
        </el-form-item>
        <el-form-item class="right">
          <el-button @click="cancelAudit">取消</el-button>
          <el-button type="primary"
                     @click="confirmAudit">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 试题预览对话框 -->
    <div>
      <questions-preview :value="list"
                         :swit="swit"
                         @changeShow="show"></questions-preview>
    </div>

  </div>
</template>

<script>
// 试题预览的对话框
import QuestionsPreview from '../components/questions-preview.vue'
import { simple } from '@/api/hmmm/subjects.js'
import { list } from '@/api/hmmm/directorys.js'
import { list as tags } from '@/api/hmmm/tags.js'
import { direction } from '@/api/hmmm/constants.js'
import { list as users } from '@/api/base/users.js'
import { datas as citys } from '@/api/hmmm/citys.js'
import { choice, choicePublish, choiceCheck, choiceAdd } from '@/api/hmmm/questions.js'

export default {
  data () {
    return {
      // 下拉框
      form: {
        // 当前页码
        page: 1,
        // 每页个数
        pagesize: 10,
        // 学科
        subjectID: null,
        // 二级目录
        catalogID: null,
        // 标签
        tag: null,
        // 试题类型
        questionType: null,
        // 难度
        difficulty: null,
        // 方向
        direction: null,
        // 录入人
        creatorID: [],
        // 城市
        city: null,
        // 省份
        province: null,
        // 关键字
        keyword: '',
        // 题目备注
        remarks: '',
        // 企业简介
        shortName: '',
        // 选项卡
        chkState: null
      },
      activeName: 'first',
      // 总条数
      total: 0,
      // 学科
      subjectIDs: [],
      // 二级目录
      catalogIDs: [],
      // 标签
      tags: [],
      // 方向
      directions: [],
      // 录入人
      creatorIDs: [],
      // 城市
      citys: null,
      // 省份
      provinces: null,
      // 题库列表
      questions: [],
      // 审核状态
      chkState: null,
      // 审核弹窗的弹出和隐藏
      dialogVisible: false,
      // 要审核的数据项
      audits: {
        id: '',
        // 是否通过
        chkState: '1',
        // 原因
        chkRemarks: ''
      },
      // 给子组件传的参
      list: {},
      // 控制子组件开关
      swit: false,
      // 删除功能需要的数据
      delQues: {
        // 要移出的id
        id: '',
        // 移出
        choiceState: 0
      }
    }
  },
  // 点击预览的组件
  components: {
    QuestionsPreview
  },
  async created () {
    // 获取方向
    this.directions = direction
    // 获取学科
    const { data } = await simple()
    this.subjectIDs = data
    // 获取管理员
    const {
      data: { list }
    } = await users()
    this.creatorIDs = list
    // 获取城市
    this.citys = citys
    // 渲染题库列表
    this.getQuestions()
  },
  methods: {
    // 清除功能
    resetForm (form) {
      this.$refs[form].resetFields()
      this.form.province = null
      this.getQuestions()
    },
    // 点击搜索
    serQuestion () {
      this.form.page = 1
      this.getQuestions()
    },
    // 监听 子组件弹窗关闭后触发，有子组件调用
    show (data) {
      if (data === 'false') {
        this.swit = false
      } else {
        this.swit = true
      }
    },
    // 点击预览
    handleClick (row) {
      this.swit = true
      this.list = row
    },
    // 点击确认审核
    async confirmAudit () {
      if (this.audits.chkRemarks) {
        this.audits.chkState = Number(this.audits.chkState)
        const a = await choiceCheck(this.audits)
        console.log(a)
        this.$message.success('审核成功')
        this.getQuestions()
        this.cancelAudit()
      } else {
        this.$message.warning('请输入审核意见')
      }
    },
    // 点击取消审核
    cancelAudit () {
      this.auditText = ''
      this.dialogVisible = false
      this.auditopinion = true
    },
    // 点击修改功能
    modify (id) {
      this.$router.push('/questions/new?id=' + id)
    },
    //  删除
    del (row) {
      this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.delQues.id = row.id
          await choiceAdd(this.delQues)
          this.getQuestions()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 点击上下架
    shelves (row) {
      const text1 = '您确认上架这道题目吗?'
      const text2 = '您确认下架这道题目吗?'
      this.$confirm(row.publishState === 1 ? text2 : text1, '题目审核', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          if (row.publishState === 1) {
            row.publishState = 0
          } else {
            row.publishState = 1
          }
          const data = await choicePublish(row)
          this.getQuestions()
          console.log(data)
          this.$message({
            type: 'success',
            message: row.publishState === 1 ? '上架成功' : '下架成功'
          })
        })
        .catch(() => { })
    },
    // 点击审核
    audit (id) {
      this.dialogVisible = true
      this.audits.id = id
    },
    // 新增试题按钮
    addQuestion () {
      this.$router.push('/questions/new')
    },
    // 标签切换事件
    switchTad () {
      this.form.chkState = this.activeName
      if (this.activeName == 'first') this.form.chkState = null
      this.form.page = 1
      this.getQuestions()
    },
    // 渲染题库列表方法
    async getQuestions () {
      try {
        const { data } = await choice(this.form)
        this.questions = data.items
        this.total = data.counts
        console.log(data)
      } catch (e) {
        console.log(e)
      }
    },
    // 当学科选项改变
    async changeBranch (a) {
      try {
        const {
          data: { items }
        } = await list({ subjectID: a })
        this.catalogIDs = items
      } catch (e) {
        console.log(e)
      }
      //  发请求获取标签
      try {
        const {
          data: { items }
        } = await tags({ subjectID: a })
        this.tags = items
      } catch (e) {
        console.log(e)
      }
    },
    // 当城市改变选择省份
    changeCity (city) {
      this.provinces = this.citys.find((item) => {
        return item.city == city
      }).area
      this.form.province = null
    },
    // 分页功能
    changepage (p) {
      this.form.page = p
      // 渲染题库列表
      this.getQuestions()
    }
  }
}
</script>

<style scoped>
.formtable{
  margin-top: 10px;
}
.box-card {
  margin-bottom: 10px;
}
.pink {
  font-size: 12px;
  color: pink;
}
.demo-form-inline {
  margin: 30px 10px;
}
el-form-item {
  width: 200px;
}
.el-form-item__label {
  font-size: 16px;
  width: 80px;
}
.el-form-item--medium {
  margin-left: 20px;
}
.el-input__inner {
  width: 300px;
}
.city .el-input__inner {
  width: 145px;
}
.block {
  margin: 20px 10px;
  float: right;
}
.block .el-input__inner {
  width: 80px;
}
.right {
  margin-top: 30px;
  text-align: right;
}
</style>
